<?php
declare(strict_types=1);

namespace App\lib;

final class Ip
{
   private $ip;

   private function __construct(string $ip)
   {
       $this->isValidIp($ip);
       $this->ip = $ip;
   }

   public static function fromString(string $ip): self
   {
       return new self($ip);
   }

   public function __toString(): string
   {
       return $this->ip;
   }

   private function isValidIp(string $ip): void
   {
       if (!filter_var($ip, FILTER_VALIDATE_IP)) {
           throw new \InvalidArgumentException(
               sprintf(
                   '"%s" is not a valid ip address',
                   $ip
               )
           );
       }
   }
}